package org.rabbitmq.test.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: Spring_RabbitMQ_Test
 * @author: chen yizhen
 * @create: 2025-03-11 11:06
 **/
@Configuration
public class InitRabbitConfig {

    /**
     * 如果没有调用@RabbitListener(queues = "test.queue")，则需要手动调用rabbitAdmin.initialize();
     * @return
     */
//    @Autowired
//    private RabbitAdmin rabbitAdmin;
//
//    @PostConstruct
//    public void init(){
//        System.out.println("强制初始化 RabbitAdmin...");
//        rabbitAdmin.initialize();
//    }

    @Bean
    public Queue testQueue(){
        return new Queue("test.queue", true, false, false);
    }

    @Bean
    public Exchange testExchange(){
        TopicExchange exchange = new TopicExchange("test.exchange", true, false);
        return exchange;
    }

    @Bean
    public Binding testBinding(){
        Binding binding = new Binding("test.queue", Binding.DestinationType.QUEUE,
                "test.exchange",
                "test.routing.key",
                null);
        return binding;
    }

    @Bean
    public Queue initDelayQueue(){
        Map<String, Object> arguments = new HashMap();
        arguments.put("x-dead-letter-exchange", "test.over.time.exchange");
        arguments.put("x-dead-letter-routing-key", "test.over.time.routing-key");
        arguments.put("x-message-ttl", 60000);
        return new Queue("test.delay.queue", true, false, false, arguments);
    }

    @Bean
    public Exchange testDelayExchange(){
        TopicExchange delayExchange = new TopicExchange("test.over.time.exchange",true, false);
        return delayExchange;
    }

    @Bean
    public Queue initOverTimeQueue(){
        Queue queue = new Queue("test.over.time.queue", true, false, false);
        return queue;
    }

    @Bean
    public Exchange testSendExchange(){
        TopicExchange sendExchange = new TopicExchange("test.send.exchange",true, false);
        return sendExchange;
    }

    @Bean
    public Binding sendBind(){
        return new Binding("test.delay.queue", Binding.DestinationType.QUEUE,
                "test.send.exchange",
                "test.send.routing-key",
                null);
    }

    @Bean
    public Binding testOverTimeBind() {
        return new Binding(
                "test.over.time.queue",      // 目标队列名
                Binding.DestinationType.QUEUE,         // 绑定类型（队列）
                "test.over.time.exchange",                 // 死信交换机名
                "test.over.time.routing-key",          // 路由键（与死信配置一致）
                null
        );
    }
}
